package com.handu.luna.core.domain.goods;

import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.jpa.domain.Specification;

import com.handu.luna.core.domain.BasePersistentDomain;

@Entity
public class GoodsAttribute  extends BasePersistentDomain<SKURepository>{
	
	private long goodsId;//
	private long attributeId;//
	private String attributeValues;//
	private String attributeName;//
	private String attributePic;//


	public GoodsAttribute() {}

	public GoodsAttribute(long goodsId, long attributeId,
			String attributeValues, String attributeName, String attributePic) {
		super();
		this.goodsId = goodsId;
		this.attributeId = attributeId;
		this.attributeValues = attributeValues;
		this.attributeName = attributeName;
		this.attributePic = attributePic;
	}



	public long getGoodsId() {
		return goodsId;
	}


	public void setGoodsId(long goodsId) {
		this.goodsId = goodsId;
	}


	public long getAttributeId() {
		return attributeId;
	}


	public void setAttributeId(long attributeId) {
		this.attributeId = attributeId;
	}


	public String getAttributeValues() {
		return attributeValues;
	}


	public void setAttributeValues(String attributeValues) {
		this.attributeValues = attributeValues;
	}


	public String getAttributeName() {
		return attributeName;
	}


	public void setAttributeName(String attributeName) {
		this.attributeName = attributeName;
	}


	public String getAttributePic() {
		return attributePic;
	}


	public void setAtteibutePic(String attributePic) {
		this.attributePic = attributePic;
	}
	
	
	/**
	 * 查询包含指定属性值的记录
	 * @param goodAttributes 属性id_属性特定值
	 * @return
	 */
	public static Specification<GoodsAttribute> searchCondition(String goodAttributeValues,Set<Long> qualifiedGoodsIds){
		
		return new Specification<GoodsAttribute>() {
			
			@Override
			public Predicate toPredicate(Root<GoodsAttribute> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate = cb.conjunction();
				List<Expression<Boolean>> expressions = predicate.getExpressions();
				
				expressions.add(cb.equal(root.<String>get("attributeValues"), goodAttributeValues));
				
				if(CollectionUtils.isNotEmpty(qualifiedGoodsIds)){
					expressions.add(root.get("").in(qualifiedGoodsIds));
				}
				return query.where(predicate).getRestriction();
			}
		};
		
	}
	
	
	
	
}